package self.hoy.demo.cloud.nacos.service;

import org.springframework.stereotype.Service;
import self.hoy.demo.cloud.entities.Payment;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 三共小不
 * @date 2020/5/29
 */
@Service
public class PaymentServiceImpl {

    private static Map<Long, Payment> database = new ConcurrentHashMap<>();

    public int create(Payment param) {
        if (param.getId() == null) {
            synchronized (database) {
                Random r = new Random();
                do {
                    param.setId(r.nextLong());
                } while (database.containsKey(param.getId()));
            }
        }
        database.put(param.getId(), param);
        return param.getId().intValue();
    }

    public Payment getPaymentById(Long id) {
        return database.get(id);
    }
}
